<?php
/**
 * Created by PhpStorm.
 * User: 吴登科
 * Date: 2018/10/16
 * Time: 9:07
 * 发票管理
 */

namespace Admin\Controller;

use Think\Image;
use Think\Upload;

class InvoiceController extends PublicController
{
    function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 发票列表
     */
    public function index()
    {
        $status = I('post.invoiceStatus');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');

        $wadmin = session("wadmin");
        if ($wadmin['store_id'] == 0) {
            $where = '';
        } else {
            $where['store_id'] = $wadmin['store_id'];
        }

        if ($status == '') {
            $status = -1;
        }

        if ($status != -1) {
            $where['status'] = $status;
        }

        if ($start_time && $end_time) {
            $where['create_time'] = array(array('egt', strtotime($start_time)), array('elt', strtotime($end_time)), 'AND');
        }

        $count = M("invoice")
            ->where($where)
            ->order('create_time desc')
            ->count();

        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show(); // 分页显示输出
//        dump($where);
        $result = M("invoice")
            ->field("invoice_id,store_id,customer_id,invoice_type,invoice_money,invoice_title,invoice_content,invoice_rate,taxpayer,status,atime,create_time,invoice_title_type,is_einvoice")
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('create_time desc')
            ->select();

        foreach ($result as $key => $v) {
            switch ($v['invoice_type']) {
                case 1:
                    $result[$key]['invoice_type'] = '企业增值税普通发票';
                    break;
                case 2:
                    $result[$key]['invoice_type'] = '增值税专用发票';
                    break;
                case 3:
                    $result[$key]['invoice_type'] = '组织增值税普通发票';
                    break;
                default:
                    $result[$key]['invoice_type'] = '企业增值税普通发票';
            }
            if($result[$key]['invoice_title_type']==1)$result[$key]['invoice_type']='增值税普通发票';

            //店铺名称
            $store_name = M('store')->field('store_name')->where(['store_id' => $v['store_id']])->find();
            $result[$key]['store_name'] = $store_name['store_name'];
            //用户名
            $username = M('user')->field('nickname')->where(['customer_id' => $v['customer_id']])->find();
            $result[$key]['username'] = $username['nickname'];
        }
        $this->assign("invoiceStatus", $status);
        $this->assign("start_time", $start_time);
        $this->assign("end_time", $end_time);
        $this->assign("result", $result);
        $this->assign("page", $show); // 赋值分页输出
        $this->display();
    }

    /**
     * 发票详情信息
     */
    public function detail()
    {
        $invoice_id = I('get.id');//发票id
        //取发票基本信息
        $invoice_info = M('invoice')->where(['invoice_id' => $invoice_id])->find();
        $invoice_info['username'] = M('user')->where(['customer_id' => $invoice_info['customer_id']])->getField('nickname');
        $invoice_info['store_name'] = M('store')->where(['store_id' => $invoice_info['store_id']])->getField('store_name');
        $arr = array(
            1 => '企业增值税普通发票',
            2 => '增值税专用发票',
            3 => '组织增值税普通发票'
        );
        $invoice_info['invoice_type'] = $arr[$invoice_info['invoice_type']];
        if($invoice_info['invoice_title_type']==1)$invoice_info['invoice_type']='增值税普通发票';
        $invoice_info['invoice_title_type'] ==1 ? $invoice_info['invoice_title_type'] = '个人' : $invoice_info['invoice_title_type'] = '企业';
        $invoice_info['create_time'] = date('Y-m-d', $invoice_info['create_time']);
        $invoice_info['atime'] = date('Y-m-d', $invoice_info['atime']);

        //申请开票订单信息
        $order_ids_arr = explode(',', $invoice_info['order_ids']);
        foreach ($order_ids_arr as $order_id) {
            $order_info[] = M('dishes_order')->field('order_no,total_price,time')->where(['id' => $order_id])->order('time desc')->find();
        }
//        dump($order_info);die;
//        dump($invoice_info);die;
        $this->assign("invoice_info", $invoice_info);
        $this->assign("order_info", $order_info);
        $this->display();
    }

    /**
     * 去开票(第三方平台接口)
     */
    public function makeInvoice()
    {
        echo '111';
    }


    /**
     * 编辑页面
     */
    public function edit()
    {
        $id = I('get.id');
        $result = M('invoice')->where("invoice_id='$id'")->find();
        $this->assign('result', $result);
        $this->display();
    }

    /**
     * 编辑操作
     */
    public function save()
    {
        $invoice_id= I('post.invoice_id');
        $data['invoice_type'] = I('post.invoice_type');
        $data['invoice_title_type'] = I('post.invoice_title_type');
        $data['invoice_money'] = I('post.invoice_money');
        $data['invoice_title'] = I('post.invoice_title');
        $data['invoice_content'] = I('post.invoice_content');
        $data['email'] = I('post.email');
        $data['address'] = I('post.address');
        $data['taxpayer'] = I('post.taxpayer');
        $data['company_address'] = I('post.company_address');
        $data['company_phone'] = I('post.company_phone');
        $data['bank'] = I('post.bank');
        $data['bank_account'] = I('post.bank_account');
        $data['is_einvoice'] = I('post.is_einvoice');

        if ($invoice_id) {
            //修改
            $result = M('invoice')->where("invoice_id='$invoice_id'")->save($data);
        }
        if ($result) {
            $this->success("操作成功", U('Admin/invoice/index'));
        } else {
            $this->error("操作失败", U('Admin/invoice/index'));
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        $id = I("get.id");
        $result = M("invoice")->where(array("invoice_id" => $id))->delete();
        if ($result !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("操作失败");
        }
    }

    /*******************************************************************************************************************
     * 发票抬头列表
     ******************************************************************************************************************/
    public function invoice_title_list()
    {
        $wadmin = session("wadmin");
        if ($wadmin['store_id'] == 0) {
            $where = '';
        } else {
            $where['store_id'] = $wadmin['store_id'];
        }

        $count = M("invoice_title")
            ->where($where)
            ->order('create_time desc')
            ->count();

        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show(); // 分页显示输出

        $result = M("invoice_title")
            ->field('*')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('create_time desc')
            ->select();

        foreach ($result as $key => $v) {
            //店铺名称
            $store_name = M('store')->field('store_name')->where(['store_id' => $v['store_id']])->find();
            $result[$key]['store_name'] = $store_name['store_name'];
            //用户名
            $username = M('user')->field('nickname')->where(['customer_id' => $v['customer_id']])->find();
            $result[$key]['username'] = $username['nickname'];
        }

        $this->assign("result", $result);
        $this->assign("page", $show); // 赋值分页输出
        $this->display();
    }
}